package com.gem.growingtree.activity;

import java.security.PublicKey;

import org.json.JSONObject;

import com.gem.growingtree.service.UserService;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnFocusChange;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@ContentView(R.layout.activity_login)
public class LoginActivity extends Activity {
	// 获取三个对象
	@ViewInject(R.id.login_name)
	private EditText login_name;
	@ViewInject(R.id.login_password)
	private EditText login_password;
	@ViewInject(R.id.loginButton)
	private Button loginButton;
	private HttpUtils httpUtils;

	@ViewInject(R.id.login_register)
	private TextView login_register;

	@ViewInject(R.id.check)
	private CheckBox check;

	// 用户保存密码
	SharedPreferences sp;
	SharedPreferences.Editor editor;

	@OnClick(R.id.loginButton)
	public void checkUser(View view) {
		final String uname = login_name.getText().toString();
		final String upass = login_password.getText().toString();

		Log.i("AA", uname + "   " + upass);
		// 用户保存密码
		if (check.isChecked()) {
			editor.putString("USER_NAME", uname);
			editor.putString("PASSWORD", upass);
			editor.commit();
		} else {
			editor.remove("USER_NAME");
			editor.remove("PASSWORD");
			editor.commit();
		}
		Log.i("AA", "A$$$$$$$$$$AA");

		new Thread() {
			public void run() {

				Log.i("AA", "AAAAAAAAAA");
				try {
					final String result = UserService.checkUser(uname, upass);
					JSONObject jsonObject = new JSONObject(result);
					if (jsonObject.get("success").equals("1")) {
						runOnUiThread(new Runnable() {
							public void run() {

								// 跳转到主页
								Intent intent = new Intent(LoginActivity.this,
										GrowthZoneActivity.class);
								startActivity(intent);
							}
						});
					} else {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								// TODO Auto-generated method stub
								Toast.makeText(LoginActivity.this,
										"timeout,try again", 1).show();
							}
						});
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}

	// @OnFocusChange(R.id.login_name)
	// public void loginName() {
	//
	// }

	// // 记住密码
	// @OnClick(R.id.check)
	// public void save(View v) {
	// if (!check.isChecked()) {
	// editor.remove("USER_NAME");
	// editor.remove("PASSWORD");
	// editor.commit();
	// }
	// }

	// 注册
	@OnClick(R.id.login_register)
	public void toRegister(View v) {
		Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
		startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_login);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		ViewUtils.inject(this);

		// if (login_name.getText().toString().equals("")) {
		// login_name.setError("用户名不能为空！");
		// login_name.requestFocus();
		// // 将显示查询结果的TextView清空
		// login_name.setText("");
		// return;
		// } else if (login_password.getText().toString().equals("")) {
		// login_password.setError("密码不能为空！");
		// login_password.requestFocus();
		// // 将显示查询结果的TextView清空
		// login_password.setText("");
		// return;
		// }

		// 获取只能被自己读写的对象
		sp = getSharedPreferences("userinfo", MODE_WORLD_WRITEABLE);
		editor = sp.edit();

		// 读取字符串
		String nameString = sp.getString("USER_NAME", null);
		String passString = sp.getString("PASSWORD", null);

		if (nameString != null && passString != null) {
			login_name.setText(nameString);
			login_password.setText(passString);
		} else {
			System.out.println("注册成功！！！！！！！！！！");
			// 注册成功跳转过来
			Intent intent = getIntent();
			String phoneString = intent.getStringExtra("phone");
			String pasString = intent.getStringExtra("password");
			login_name.setText(phoneString);
			login_password.setText(pasString);

		}

		// 保存cookie
		// httpUtils = new HttpUtils();
		// httpUtils.configCookieStore(new PreferencesCookieStore(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

}
